package com.lzp.mongodemo.converter;

import org.bson.Document;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.core.convert.converter.Converter;

/**
 * @description: 自定义Money转换类.Mongo BSON类型与Money类型
 * @author: lzp
 * @createTime: 2021-12-05 23:32
 **/
public class MoneyReadConverter implements Converter<Document, Money> {
    @Override
    public Money convert(Document source) {
        // 获取bson中money
        Document money = (Document) source.get("money");
        // 解析出Money的两个属性
        double amount = Double.parseDouble(money.getString("amount"));
        String currentcy = ((Document) money.get("currency")).getString("code");
        return Money.of(CurrencyUnit.of(currentcy), amount);
    }
}
